# -*- coding: utf-8 -*-
# @Time    : 2022/3/18 15:14

from src.ApiTest.ConfigurationData.Database.data_database import TestData, TestDataSchema
from src.User.Database.user_database import User, UserSchema
from Common.yaml_method import YamlMethod
import ast
from sqlalchemy import and_


class DataList:
    """
    获取测试数据列表接口

    """

    @staticmethod
    def data_list(page, limit, data_name, user_id, project_name):
        """
        获取测试数据列表接口
        :param page: 页码
        :param limit: 每页多少条数据
        :param data_name: 参数名称
        :param user_id: user_id
        :param project_name: 项目名称
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        if project_name:
            if data_name:
                # projectId和data_name都不为空
                data = TestData.query.filter_by(data_name=data_name, projectName=project_name)\
                    .paginate(page=page, per_page=limit)
            else:
                # projectId不为空，data_name都为空
                data = TestData.query.filter_by(projectName=project_name).paginate(page=page, per_page=limit)
        else:
            # 先查询用户所属项目信息
            user = User.query.filter_by(id=user_id).first()
            user_schema = UserSchema()
            user_data = user_schema.dump(user)
            user_project_id = user_data['user_projects']
            id_list = ast.literal_eval(user_project_id)
            # 开始构建过滤条件
            filters = [TestData.projectId.in_(id_list)]
            if data_name:
                # projectId为空，data_name不为空
                filters.append(TestData.data_name == data_name)
                data = TestData.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
            else:
                # projectId和data_name都为空
                data = TestData.query.filter(TestData.projectId.in_(id_list)).paginate(page=page, per_page=limit)

        info = []
        if data:
            for i in data.items:
                data_schema = TestDataSchema()
                data_info = data_schema.dump(i)
                # 将单条信息添加到info中
                info.append(data_info)

        total = data.total
        has_more = data.has_next
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'dataList': info,
                'total': total,
                'has_more': has_more
            }
        }

        return res

    @staticmethod
    def data_name(page, limit):
        """
        获取测试数据名称接口
        :param page: 页码
        :param limit: 每页多少条数据
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        data = TestData.query.filter().paginate(page=page, per_page=limit)
        info = []
        if data:
            for i in data.items:
                data_schema = TestDataSchema()
                data_info = data_schema.dump(i)['data_name']
                # 将单条信息添加到info中
                info.append(data_info)

        total = data.total
        has_more = data.has_next
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'dataList': info,
                'total': total,
                'has_more': has_more
            }
        }

        return res
